// 二叉苹果树
#include <cstring>
#include <iostream>

using namespace std;
const int N = 110, M = N << 1;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
int f[N][N];
void add(int a, int b, int c) {
  e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int father) {
  for (int i = h[u]; ~i; i = ne[i]) {
    int v = e[i];
    if (v == father) continue;
    dfs(v, u);
    for (int j = m; j >= 0; --j) {
      for (int k = 0; k <= j - 1; ++k) {
        // 枚举体积
        // 这里k-1是为了留一条连向父节点的边
        f[u][j] = max(f[u][j], f[u][j - k - 1] + f[v][k] + w[i]);
      }
    }
  }
}

int main() {
  memset(h, -1, sizeof h);
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    add(a, b, c);
    add(b, a, c);
  }
  dfs(1, -1);
  cout << f[1][m] << endl;
  return 0;
}
